package com.shao.service.Impl;


import com.shao.bean.admin;
import com.shao.bean.cls;
import com.shao.bean.student;
import com.shao.bean.teacher;
import com.shao.common.R;
import com.shao.common.admStuInfo;
import com.shao.common.kes;
import com.shao.dao.admLoginDao;
import com.shao.dao.stuLoginDao;
import com.shao.dao.teaDao;
import com.shao.service.admService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: com.song shao
 * @date: 2022/2/27
 * @time: 11:34
 * @version: 1.0
 */
@SuppressWarnings("all")
@Service
public class admServiceImpl implements admService {

    @Autowired
    private stuLoginDao sld;

    @Autowired
    private admLoginDao adm;

    @Autowired
    private teaDao tea;

    /**
     * 管理员的登录验证
     *
     * @param loginPeopel
     * @return
     */
    @Override
    public R loginVerify(admin loginPeopel) {
        String name = loginPeopel.getAdm_username();  // 账号
        String pwd = loginPeopel.getAdm_password();    // 密码
        if (!(name.length() > 0 && name.length() == 11 && pwd.length() > 6)) {
            return R.error("请检查你的账号是否是（11位）,密码不能小于6位！");
        }
        admin accountStatus = adm.findStas(name);
        if (accountStatus == null) {
            return R.error("你的账号不存在！");
        }
        String stu_pwd = accountStatus.getAdm_password(); // 数据库中的密码
        if (!pwd.equals(stu_pwd)) {
            return R.error("你的账号或密码不正确(其实是你的密码!)");
        }
        return R.success(333);
    }

    /**
     * 查询管理员的钩子函数发送的请求
     *
     * @return
     */
    @Override
    public R<List<admStuInfo>> admFindStudentInfo() {
        List<admStuInfo> studentVos = new ArrayList<>();                        // 返回的         vo
        List<student> stuResult = sld.findStudentList();                        // 拿到学生
        for (student student : stuResult) {
            admStuInfo admStuInfo = new admStuInfo();
            admStuInfo.setStu_username(student.getStu_username());              // 设置账号
            admStuInfo.setStu_nikename(student.getStu_nikename());              // 设置学生的名字          1
            admStuInfo.setStu_state(student.getStu_state());                                       // 拿到学生的账号的状态     2
            String stu_info = student.getStu_info();                            // 拿到学生详情表的id
            List<com.shao.bean.stu_info> stuData = sld.findStuData(stu_info);   // 拿到了学生信息表
            cls aClass = sld.findClass(stuData.get(0).getStu_class_id());       //班级对象
            admStuInfo.setClass_name(aClass.getClass_name());
            studentVos.add(admStuInfo);
        }
        return R.success(studentVos);
    }

    /**
     * 逻辑删除账号的（其实就是封禁）
     *
     * @param stu_username
     * @return
     */
    @Override
    public R deleteStu(kes stu_username) {
        String stu_username1 = stu_username.getStu_username();
        // 先确认谁是老师
        student stu = sld.findStudent(stu_username1);
        teacher tresu;
        if (stu == null) {
            tresu = tea.findByTeacher(stu_username1);
            if (tresu == null) {
                return R.error("系统繁忙，请稍后重试！");
            } else {
                // 这个是老师
                int tea_state = tresu.getTea_state();
                if (tea_state == 0) {
                    int kes = tea.updataStatus(stu_username1, 1);
                    return R.success("修改成功！");
                } else {
                    int kes = tea.updataStatus(stu_username1, 0);
                    return R.success("修改成功！");
                }
            }
        }
        int stu_state = stu.getStu_state(); // 状态
        if (stu_state == 0) {
            int res = sld.updataStatus(stu_username1, 1);
            return R.success("修改成功！");
        } else {
            int res = sld.updataStatus(stu_username1, 0);
            return R.success("修改成功！");
        }
    }

    /**
     * 查询管理员的信息
     *
     * @return
     */
    @Override
    public List<admin> findAdmInfo() {
        List<admin> resu = adm.selectAdmin();
        return resu;
    }
}
